﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudReader.Design.Struct
{
    public struct HSL
    {
        public HSL(System.Windows.Media.Color color)
        {
            var R = color.R / 255f;
            var G = color.G / 255f;
            var B = color.B / 255f;
            var max = R > G ? (R > B ? R : B) : (G > B ? G : B);
            var min = R < G ? (R < B ? R : B) : (G < B ? G : B);

            var pl = max - min;
            var ph = max + min;
            if (max == min)
            {
                H = 0;
            }
            else if (max == R && G >= B)
            {
                H = 60 * ((G - B) / pl) + 0;
            }
            else if (max == R && G < B)
            {
                H = 60 * ((G - B) / pl) + 360;
            }
            else if (max == G)
            {
                H = 60 * ((B - R) / pl + 120);
            }
            else
            {
                H = 60 * ((R - G) / pl + 240);
            }

            L = 1 / 2f * (ph);
            const float cc = 1 / 2f;

            if (L == 0 || max == min)
            {
                S = 0f;
            }
            else if (0 < L && L >= cc)
            {
                S = pl / ph;
            }
            else
            {
                S = pl / (2 - ph);
            }
        }
        public float H { get; set; }
        public float S { get; set; }
        public float L { get; set; }
        internal System.Windows.Media.Color GetRGB()
        {
            float q;
            var half = 1 / 2f;
            if (L < half)
            {
                q = L * (1 + S);
            }
            else
            {
                q = L + S - (L * S);
            }

            float p = 2 * L - q;

            var Hk = H / 360;
            var so = 1 / 3f;
            var tR = Hk + so;
            var tG = Hk;
            var tB = Hk - so;

            if (tR < 0)
            {
                tR += 1.0f;
            }
            else if (tR > 1)
            {
                tR -= 1.0f;
            }

            if (tG < 0)
            {
                tG += 1.0f;
            }
            else if (tG > 1)
            {
                tG -= 1.0f;
            }

            if (tB < 0)
            {
                tB += 1.0f;
            }
            else if (tB > 1)
            {
                tB -= 1.0f;
            }

            var color = new System.Windows.Media.Color();

            if (tR < (1 / 6f))
            {
                color.R =Convert.ToByte( p + ((q - p) * 6 * tR));
            }
            else if ((1 / 6f) <= tR && tR < (1 / 2f))
            {
                color.R = Convert.ToByte(q);
            }
            else if ((1 / 2f) <= tR && tR < (2 / 3f))
            {
                color.R = Convert.ToByte(p + ((q - p) * 6 * ((2/3f)-tR)));
            }
            else
            {
                color.R = Convert.ToByte(p);
            }

            if (tG < (1 / 6f))
            {
                color.G = Convert.ToByte(p + ((q - p) * 6 * tG));
            }
            else if ((1 / 6f) <= tG && tG < (1 / 2f))
            {
                color.G = Convert.ToByte(q);
            }
            else if ((1 / 2f) <= tG && tG < (2 / 3f))
            {
                color.G = Convert.ToByte(p + ((q - p) * 6 * ((2 / 3f) - tG)));
            }
            else
            {
                color.G = Convert.ToByte(p);
            }

            if (tB < (1 / 6f))
            {
                color.B = Convert.ToByte(p + ((q - p) * 6 * tB));
            }
            else if ((1 / 6f) <= tB && tB < (1 / 2f))
            {
                color.B = Convert.ToByte(q);
            }
            else if ((1 / 2f) <= tB && tB < (2 / 3f))
            {
                color.B = Convert.ToByte(p + ((q - p) * 6 * ((2 / 3f) - tB)));
            }
            else
            {
                color.B = Convert.ToByte(p);
            }

            return color;
        }
    }
}
